package com.example.bysj_sb.dao;


import com.example.bysj_sb.domain.Degree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("degreeDao")
public class DegreeDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //增加
    public boolean add(Degree degree) {
        String sql = "INSERT INTO degree (no,description,remarks) VALUES (?,?,?)";
        int affectedRowNum = jdbcTemplate.update(sql, degree.getNo(), degree.getDescription(),degree.getRemarks());
        if (affectedRowNum != 1) {
            return false;
        } else {

            return true;
        }
    }
    //根据id查询
    public Degree find(int id){
        String sql = "SELECT * FROM degree WHERE id = ?";
        //查询表的结果与实体类属性一致，可用BeanPropertyRowMapper直接生成相应对象
        List<Degree> degrees = jdbcTemplate.query(sql, new Object[]{id},
                new BeanPropertyRowMapper<>(Degree.class));
        if(!degrees.isEmpty()){
            return degrees.get(0);
        } else
            return null;
    }
    //查询全部
    public List<Degree> findAll(){
        String sql = "SELECT * FROM degree";
        List<Degree> degrees = jdbcTemplate.query(sql,
                new BeanPropertyRowMapper<>(Degree.class){

        });
        return degrees;
    }
    //修改
    public boolean update(Degree degree){
        String sql = "UPDATE degree SET no = ? ,description=? WHERE id = ?";
        int affectedRowNum = jdbcTemplate.update(sql,degree.getNo(),
                degree.getDescription(),degree.getId());
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
    //删除
    public boolean delete(int id){
        String sql = "DELETE FROM degree WHERE id = ? ";
        int affectedRowNum = jdbcTemplate.update(sql,id);
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
}
